
const u = navigator.userAgent
const ua = navigator.userAgent.toLowerCase()
const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
// eslint-disable-next-line no-undef
window.flv = flvjs
function hasClass(ele, cls) {
  return !!ele.className.match(new RegExp(`(\\s|^)${cls}(\\s|$)`))
}

const utils = {
  isIos: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
  isWeixin: ua.match(/MicroMessenger/i) === 'micromessenger',
  isQQ: u.match(/\sQQ/i) === ' QQ',
  isAndroid: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
  isHuwei:
    navigator.userAgent.toLowerCase().indexOf('huawei') > -1
    || navigator.userAgent.toLowerCase().indexOf('honor') > -1,
  // eslint-disable-next-line func-names
  flvCommon: (function () {
    const flvSupport = (function flvSupport() {
      // eslint-disable-next-line no-undef
      const flvSuppot = flv.getFeatureList().mseLiveFlvPlayback && !isIOS
      return flvSuppot
    }())
    function createFlvPlayer(url, videoElement) {
      // eslint-disable-next-line no-undef
      const flvPlayer = flv.createPlayer({
        type: 'flv',
        url,
        isLive: true
      })
      flvPlayer.attachMediaElement(videoElement)
      return flvPlayer
    }
    function destroyFlvPlayer(flvPlayer) {
      flvPlayer.pause()
      flvPlayer.unload()
      flvPlayer.detachMediaElement()
      flvPlayer.destroy()
    }
    return {
      flvSupport,
      createFlvPlayer,
      destroyFlvPlayer
    }
  }()),
  toggleClass(el, className) {
    className.split(/\s+/g).forEach(item => {
      if (hasClass(el, item)) {
        this.removeClass(el, item)
      } else {
        this.addClass(el, item)
      }
    })
  },
  removeClass(el, className) {
    if (el.classList) {
      className.split(/\s+/g).forEach(item => {
        el.classList.remove(item)
      })
    } else if (hasClass(el, className)) {
      className.split(/\s+/g).forEach(item => {
        const reg = new RegExp(`(\\s|^)${item}(\\s|$)`)
        // eslint-disable-next-line no-param-reassign
        el.className = el.className.replace(reg, ' ')
      })
    }
  },
  addClass(el, className) {
    if (el.classList) {
      className.replace(/(^\s+|\s+$)/g, '').split(/\s+/g).forEach(item => {
        // eslint-disable-next-line no-unused-expressions
        item && el.classList.add(item)
      })
    } else if (!hasClass(el, className)) {
      // eslint-disable-next-line no-param-reassign
      el.className += ` ${className}`
    }
  },
  loadInitLine(serverUrl, flvSupport) {
    const urlArr = []
    if (flvSupport) {
      serverUrl.forEach((item) => {
        if (item.flv && typeof (item.flv) === 'string') {
          urlArr.push(item.flv)
        }
      })
    } else {
      serverUrl.forEach((item) => {
        urlArr.push(item.hls)
      })
    }
    return urlArr
  },
  wxPlayVideo(video) {
    // eslint-disable-next-line no-undef
    WeixinJSBridge.invoke('getNetworkType', {}, () => {
      video.play().catch(err => {
        console.log(err)
      })
    })
  }
}
export default utils
